<?php if (!defined('THINK_PATH')) exit();?><div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false">
        <!-- Begin of toolbar -->
        <div id="user-toolbar">
            <div class="wu-toolbar-button">
                <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="searchBlogUserGrid()" plain="true">刷新</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-lock" onclick="batchedituserStatus(0)" plain="true">批量锁定</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-lock" onclick="batchedituserStatus(1)" plain="true">批量解锁</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="batchresetUserPwd()" plain="true">批量重置密码</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-back" onclick="removeTab()" plain="true">返回</a>
            </div>
            <div class="wu-toolbar-search">
                <label>用户昵称：</label><input class="easyui-textbox" type="text" name="bloguser"
                                           data-options="prompt:'用户昵称'" style="width:240px"></input>
                <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="searchBlogUserGrid()">开始检索</a>
            </div>
        </div>
        <!-- End of toolbar -->
        <table id="user-datagrid" toolbar="#user-toolbar" class="easyui-datagrid">
        </table>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('#user-datagrid').datagrid({
            url: "<?php echo U('User/getUserListByPage','','');?>",
            rownumbers: true,
            pagination: true,
            fitColumns: true,
            fit: true,
            singleSelect: false,
            pageSize: 10,
            pageList: [5, 20, 50],
            remoteSort: true,
            multiSort: true,
            columns: [[
                {field:'ck',checkbox:"true"},
                {field: 'id', title: 'ID', align: 'center', width: 40},
                {field:'username',title: '用户昵称',width:80,align:'center'},
                {field:'face',title: '头像',width:80,align:'center',formatter:showFace},
                {field:'follow',title: '关注人数',width:80,align:'center'},
                {field:'fans',title: '粉丝人数',width:80,align:'center'},
                {field:'weibo',title: '发表微博条数',width:80,align:'center'},
                {field:'registime',title: '注册时间',width:240,align:'center',formatter: format_registime},
                {field: 'lock', title: '账号状态', width: 60, align: 'center', formatter: showUserStatus},
                {field: 'operation', title: '操作', width: 180, align: 'center', formatter: showUserBtn}
            ]],
            queryParams: {
                username: ""
            },
            onLoadSuccess: function (data) {
                $("a[name='resetpwd']").linkbutton({plain: true, iconCls: 'icon-edit'});
                $("a[name='setlock']").linkbutton({plain: true, iconCls: 'icon-lock'});
            }
        });
    });

    //格式化图片超级链接
    function showFace(value, row, index) {
        if(value != '' && value != null){
            return "<img src='/MicroBlog/"+ value +"' style='width:30px;heigth:30px;' />";
        }else{
            return "<img src='/MicroBlog/Public/Admin/images/noface.gif' style='width:30px;heigth:30px;' />";
        }
    }

    //格式化日期列
    function format_registime(value, row, index) {
        return toDateString(value * 1000);
    }

    //显示状态
    function showUserStatus(value, row, index) {
        if (value == 0) {
            return "正常";
        } else {
            return "锁定";
        }
    }

    //显示操作按钮
    function showUserBtn(value, row, index) {
        if(row['lock'] == 0){
            return "<a href='#' onclick='resetUserPwd(" + index + ")' class='easyui-linkbutton' name='resetpwd'>重置密码</a>&nbsp;" +
                "<a href='#' onclick='edituserStatus(" + index + ")' class='easyui-linkbutton' name='setlock'>锁定用户</a>";
        }else{
            return "<a href='#' onclick='resetUserPwd(" + index + ")' class='easyui-linkbutton' name='resetpwd'>重置密码</a>&nbsp;" +
                "<a href='#' onclick='edituserStatus(" + index + ")' class='easyui-linkbutton' name='setlock'>解除锁定</a>";
        }
    }

    //按条件查询表格数据
    function searchBlogUserGrid() {
        var queryParams = $('#user-datagrid').datagrid('options').queryParams;
        queryParams.username = $.trim($('input[name=bloguser]').val());
        $('#user-datagrid').datagrid('reload');
    }

    //重置密码
    function resetUserPwd(rowIndex){
        rows = $('#user-datagrid').datagrid('getSelections');
        if (rows.length != 1){
            return;
        }
        //获取当前选中行的索引
        selectedRowIndex = $('#user-datagrid').datagrid('getRowIndex',$('#user-datagrid').datagrid('getSelected'));
        //判断当前按钮所在行与当前选中行是否一致
        if (rowIndex == selectedRowIndex){
            row = $('#user-datagrid').datagrid('getSelected');
            if (row){
                $.ajax({
                    url: "<?php echo U('User/resetPwd','','');?>",
                    type: "post",
                    dataType: "json",
                    data: 'ids=' + row['id'],
                    success: function (result) {
                        $.messager.show({
                            title:'系统提示',
                            msg:result.msg
                        });
                        $('#user-datagrid').datagrid('reload');
                    }, error: function () {
                    }
                });
            }
        }
    }

    //批量重置密码
    function batchresetUserPwd(){
        rows = $('#user-datagrid').datagrid('getSelections');
        if (rows.length == 0){
            return;
        }
        ids = "";
        for(i=0;i<rows.length;i++)
        {
            ids = ids + rows[i]['id'] + ",";
        }
        ids = ids.substring(0,ids.length-1);
        $.ajax({
            url: "<?php echo U('User/resetPwd','','');?>",
            type: "post",
            dataType: "json",
            data: 'ids=' + ids,
            success: function (result) {
                $.messager.show({
                    title:'系统提示',
                    msg:result.msg
                });
                $('#user-datagrid').datagrid('reload');
            }, error: function () {
            }
        });
    }

    //修改用户状态
    function edituserStatus(rowIndex){
        rows = $('#user-datagrid').datagrid('getSelections');
        if (rows.length != 1){
            return;
        }
        //获取当前选中行的索引
        selectedRowIndex = $('#user-datagrid').datagrid('getRowIndex', $('#user-datagrid').datagrid('getSelected'));
        //判断当前按钮所在行与当前选中行是否一致
        if (rowIndex == selectedRowIndex) {
            row = $('#user-datagrid').datagrid('getSelected');
            if (row) {
                $.ajax({
                    url: "<?php echo U('User/editUserStatus','','');?>",
                    type: "post",
                    dataType: "json",
                    data: 'ids=' + row['id'] + '&lock=' + row['lock'],
                    success: function (result) {
                        $.messager.show({
                            title:'系统提示',
                            msg:result.msg
                        });
                        $('#user-datagrid').datagrid('reload');
                    }, error: function () {
                    }
                });
            }
        }
    }

    //批量修改系统用户状态
    function batchedituserStatus(lock){
        rows = $('#user-datagrid').datagrid('getSelections');
        if (rows.length == 0){
            return;
        }
        ids = "";
        for(i=0;i<rows.length;i++)
        {
            ids = ids + rows[i]['id'] + ",";
        }
        ids = ids.substring(0,ids.length-1);
        $.ajax({
            url: "<?php echo U('User/editUserStatus','','');?>",
            type: "post",
            dataType: "json",
            data: 'ids=' + ids + '&lock=' + lock,
            success: function (result) {
                $.messager.show({
                    title:'系统提示',
                    msg:result.msg
                });
                $('#user-datagrid').datagrid('reload');
            }, error: function () {
            }
        });
    }

</script>